package cn.dhnep.demo.service.impl;

import cn.dhnep.demo.mapper.PackageOrderItemMapper;
import cn.dhnep.demo.mapper.PackageOrderMapper;
import cn.dhnep.demo.pojo.PackageOrder;
import cn.dhnep.demo.pojo.PackageOrderItem;
import cn.dhnep.demo.pojo.vo.RespPageBean;
import cn.dhnep.demo.service.PackageOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author dhn
 * @since 2021-09-06
 */
@Service
public class PackageOrderServiceImpl extends ServiceImpl<PackageOrderMapper, PackageOrder> implements PackageOrderService {

    @Resource
    private PackageOrderItemMapper packageOrderItemMapper;

    @Override
    public RespPageBean getAllPackageOrder(Integer current, Integer size, String status, String orderNumber, String horseman, String recycleNumber, String recycleName) {
        QueryWrapper<PackageOrder> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(status)) wrapper.eq("status", status); //订单状态
        if (!StringUtils.isEmpty(orderNumber)) wrapper.like("order_number", orderNumber); //订单编号
        if (!StringUtils.isEmpty(horseman)) wrapper.like("horseman_name", horseman); //骑手姓名
        if (!StringUtils.isEmpty(recycleNumber)) wrapper.like("recycle_number", recycleNumber); //回收站电话
        if (!StringUtils.isEmpty(recycleName)) wrapper.like("recycle_name", recycleName); //回收站名字
        wrapper.orderByDesc("id");

        Page<PackageOrder> page = new Page<>(current, size);
        baseMapper.selectPage(page, wrapper);

        List<PackageOrder> packageOrders = page.getRecords();
        packageOrders.forEach(packageOrder -> {
            packageOrder.setPackageOrderItems(packageOrderItemMapper.selectList(
                    new QueryWrapper<PackageOrderItem>().eq("order_id", packageOrder.getId())));
        });

        return new RespPageBean(0, page.getTotal(), packageOrders);
    }
}
